from libs.base.decorators import check_login, check_form
from libs.base.web import BaseHandler
from . import forms
from . import models


class UserListHandler(BaseHandler):

    @check_login()
    @check_form(forms.UserListForm)
    async def get(self):
        """
        @api {GET} /api/v1.0/user/user_list 获取用户列表
        @apiVersion 1.0.0
        @apiName GetUserList
        @apiGroup AuthUser
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) page  查询页码
        @apiParam (Request-params) limit  每页显示条数
        @apiParam (Request-params) user_id  用户ID
        @apiParam (Request-params) account  账号
        @apiParam (Request-params) name  用户名
        @apiParam (Request-params) email 邮箱
        @apiParam (Request-params) phone_number 手机号码


        @apiSuccess (Response-data) page  查询页码
        @apiSuccess (Response-data) limit  每页显示条数
        @apiSuccess (Response-data) user_id  用户ID
        @apiSuccess (Response-data) account  账号
        @apiSuccess (Response-data) name  用户名
        @apiSuccess (Response-data) email 邮箱
        @apiSuccess (Response-data) phone_number  手机号码
        @apiSuccess (Response-data) create_by  创建人
        @apiSuccess (Response-data) create_by.user_id  创建人的ID
        @apiSuccess (Response-data) create_by.name  创建人的姓名
        @apiSuccess (Response-data) create_time  创建时间



        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "results": [
                    {
                        "user_id": 1,
                        "account": "xxx",
                        "name": "xxx",
                        "email": "rick.lv@qq.com",
                        "phone_number": "15216783944",
                        "create_by": {
                            "user_id": 1,
                            "name": "xxx"
                        },
                        "create_time": "2019-06-24 16:46:24"
                    },
                    {
                        "user_id": 2,
                        "account": "xxx",
                        "name": "ks",
                        "email": "10342@qq.com",
                        "phone_number": "13122358800",
                        "create_by": {
                            "user_id": 1,
                            "name": "xxx"
                        },
                        "create_time": "2020-12-31 00:00:00"
                    },
                ],
                "total": 11,
                "page": 1,
                "limit": 10
            }
        }
        """

        model = models.UserListModel(self)
        res = await model.fetch_user_list(self.form_data)
        self.finish_auto(**res)


class UserInfoHandler(BaseHandler):

    """获取某一个用户信息"""
    @check_login()
    @check_form(forms.GetUserForm)
    async def get(self, user_id):
        """

        @api {GET} /api/v1.0/user/:user_id 获取用户详情
        @apiVersion 1.0.0
        @apiName GetUserInfo
        @apiGroup AuthUser
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) user_id  用户ID

        @apiSuccess (Response-data) user_id  用户ID
        @apiSuccess (Response-data) account  账号
        @apiSuccess (Response-data) name  用户名
        @apiSuccess (Response-data) email 邮箱
        @apiSuccess (Response-data) phone_number  手机号码
        @apiSuccess (Response-data) create_by  创建人
        @apiSuccess (Response-data) create_by.user_id  创建人ID
        @apiSuccess (Response-data) create_by.name  创建人姓名
        @apiSuccess (Response-data) create_time  创建时间

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "user_id": 1,
                "account": "xxxxx",
                "name": "xxxx",
                "email": "rick.lv@qq.com",
                "phone_number": "13512341234",
                "create_by": {
                    "user_id": 1,
                    "name": "xxxx"
                },
                "create_time": "2019-06-24 16:46:24",
            }
        }
        """
        model = models.UserInfoModel(self, user_id)
        res = await model.fetch_info(self.form_data)
        self.finish_auto(**res)

    @check_login()
    @check_form(forms.AddUserForm)
    async def post(self, user_id):
        """
        @api {POST} /api/v1.0/user 新增用户
        @apiVersion 1.0.0
        @apiName AddStoreInfo
        @apiGroup AuthUser
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) name  用户名
        @apiParam (Request-params) account  账户
        @apiParam (Request-params) password  密码
        @apiParam (Request-params) account  账号
        @apiParam (Request-params) email邮箱
        @apiParam (Request-params) phone_number 手机号码

        @apiSuccess (Response-data) user_id  用户ID

        @apiParamExample {json} Request-Example:
        {
            "account":"1847xxx",
            "password":"xxx",
            "name":"xxx",
            "phone_number":"131223588xx",
            "email":"xxx@xx.xxx"
        }

        @apiSuccessExample {json} Success-Response:
       {

        "status": 0,
        "message": "请求成功",
        "data": {
            "user_id": 16
                }
        }
        """
        model = models.UserInfoModel(self,user_id)

        res = await model.insert_info(self.form_data)

        self.finish_auto(**res)

    @check_login()
    @check_form(forms.AddUserForm)
    async def put(self, user_id):
        """
        @api {PUT} /api/v1.0/user/:user_id 修改用户基本信息
        @apiVersion 1.0.0
        @apiName UpdateUserInfo
        @apiGroup AuthUser
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params)   name  用户名
        @apiParam (Request-params)   account  账户
        @apiParam (Request-params)   password 密码
        @apiParam (Request-params)   account  账号
        @apiParam (Request-params)   email 邮箱
        @apiParam (Request-params)   phone_number 手机号码


        @apiParamExample {json} Request-Example:
        {
            "account":"aaa",
            "password": "xxx",
            "name": "xxx",
            "phone_number": "131223588xx",
            "email": "xxx@xxx.xxx"
        }

        @apiSuccessExample {json} Success-Response:
        {
            "status":0,
            "message":"请求成功",
            "data":{}
        }
        """
        model = models.UserInfoModel(self, user_id)
        res = await model.update_info(self.form_data)
        self.finish_auto(**res)

    async def delete(self, user_id):

        """
        @api {DELETE} /api/v1.0/user/:user_id 删除用户
        @apiVersion 1.0.0
        @apiName DeleteUserInfo
        @apiGroup AuthUser
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) user_id  用户ID

        @apiSuccessExample {json} Success-Response:
        {
            "status":0,
            "message":"请求成功",
            "data":{}
        }
        """
        model = models.UserInfoModel(self, user_id)
        res = await model.delete_info()
        self.finish_auto(**res)


class UserPermissHandler(BaseHandler):
    """权限api类"""
    @check_login()
    async def get(self):
        """

        @api {GET} /api/v1.0/user_info 获取用户权限
        @apiVersion 1.0.0
        @apiName GetUserPermsInfo
        @apiGroup AuthUsers
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiSuccess (Response-data) user_id  用户ID
        @apiSuccess (Response-data) account  账号
        @apiSuccess (Response-data) name  用户姓名
        @apiSuccess (Response-data) email  邮箱
        @apiSuccess (Response-data) phone_number  手机号码
        @apiSuccess (Response-data) create_time  账户创建时间
        @apiSuccess (Response-data) permissions  权限列表

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "user_id": 1,
                "account": "xxx",
                "name": "",
                "email": "",
                "phone_number": "",
                "create_time": "2019-06-24 16:46:24",
                "permissions": [
                    "demo_a",
                    "demo_admin",
                    "demo_b"
                ]
            }
        }
        """
        model = models.UserModel(self)
        res = await model.get_user_info()
        # res = {'status': 0, 'message': 'success', 'data': {
        #     "test": "ddd"
        # }}
        self.finish_auto(**res)


